package cn.ssm.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.ssm.biz.impl.TeacherBizImpl;
import cn.ssm.pojo.Pager;
import cn.ssm.pojo.Teacher;
import cn.ssm.pojo.User;

	@Controller
	@RequestMapping("/teacher")
	public class TeacherController {
	@Autowired
	@Qualifier("TeacherBizImpl")
	private TeacherBizImpl TBI;
	@RequestMapping("/teacherList")
	public String getTeacherList(Model model,HttpServletRequest request) {
		String currentPageStr =request.getParameter("currentPage");
		//System.out.println(currentPageStr);
		int rowCount = TBI.findTeaCount();
		Pager pager=null;
		if(currentPageStr==null){
			pager=new Pager(rowCount, 5, 1);
		}else{
			int currentPage=Integer.parseInt(request.getParameter("currentPage"));
			pager=new Pager(rowCount, 5, currentPage);
		}
		pager.setUrl("/teacher/teacherList");
		List<Teacher> tList =TBI.getTeacherList(pager);
		model.addAttribute("pager", pager);
		
		model.addAttribute("list", tList);
		System.out.println(tList);
		String url="";
		if(tList!=null){
			url="TeacherList";
		}else{
			url="404";
		}
		return url;
	}
	@RequestMapping("/delTea")
	public String deleteTeacher(Model model,Integer id) {
		Integer Tdel=TBI.deleteTeacher(id);
		model.addAttribute("del", Tdel);
		String url="";
		if(Tdel>0){
			url="redirect:/teacher/teacherList";
		}else{
			url="404";
		}
		return url;
	}

	@RequestMapping("/addTea")
	public String addTeacher(Model model,Teacher teacher) {
		System.out.println(teacher.getTeacherName());
		System.out.println(teacher.getAddress());
		int Tadd=TBI.addTeacher(teacher);
		System.out.println(Tadd);
		String url="";
		if(Tadd>0){
			url="redirect:/teacher/teacherList";
		}else{
			url="404";
		}
		return url;
	}

	@RequestMapping("/updateTea")
	public String updateTeacher(Teacher teacher,int id,@RequestParam("teacherName") String teacherName,@RequestParam("address") String address,@RequestParam("phone") String phone,@RequestParam("email") String email) {
		teacher.setId(id);
		teacher.setTeacherName(teacherName);
		teacher.setAddress(address);
		teacher.setPhone(phone);
		teacher.setEmail(email);
		//teacher.setClassroom(classroom);
		int Tupdate =TBI.updateTeacher(teacher);
		String url="";
		if(Tupdate>0){
			url="redirect:/teacher/teacherList";
		}else{
			url="404";
		}
		return url;
	}
	@RequestMapping("/getById")
	public String getTeaById(@RequestParam int id,Model model){
		Teacher tList =TBI.getTeacherListById(id);
		String url="";
		if(tList!=null){
			model.addAttribute("tList", tList);
			url="updateTeacher";
		}else{
			url="404";
		}
		return url;
	}

}
